<?php
/**
 * ProjektMan
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   ProjektMan
 * @name       ProjectController.php
 * @license    LICENSE.txt     New BSD License
 */

/**
 * ProjectController
 *
 * @category   ProjektMan
 * @name       ProjectController
 * @license    LICENSE.txt     New BSD License
 */
class ProjectController extends Projekt_Action
{

    /**
     * The index action just redirects to the list page
     * we should not directly link to this page
     *
     */
    public function indexAction ()
    {
        $this->_redirect('/project/list/');
    }

    /**
     * Action to display the current projects
     *
     */
    public function listAction ()
    {
        //get the projects
        $projectList = new ProjectList();
        $data = $projectList->fetchAll();
        //$datacount = count($data);

        //loop through the projects and generate html for output
        $output = "<div class='project'>";

        foreach ($data as $project)
        {
            $project_name = "<a href='/project/overview/project_id/" . $project->project_id . "/'>" . $project->project_name . "</a>";
            $status = $project->getStatus();
			$owner = $project->getOwner();

            $output .= "<h1>" . $project_name . " - " . $status->status_name . " - Owner:&nbsp;&nbsp;" . $owner->last_name . ", " . $owner->first_name . " - Edit - Add Task</h1>";

            $projectHistoryList = $project->getHistory();

            foreach ($projectHistoryList as $event) {
                $output .= "<div class='project_history'>" . "<span class='history_type history_" . $event->history_type . "'>" . $event->history_type . "</span> " . $event->description . "</div>";
            }
        }

        $output .= "</div>";

        $this->view->pageTitle = "Projects";
        $this->view->crumbTrail = $this->view->pageTitle;
        $this->view->output = $output;

        //die($navHtml);

    }

    /**
     * Create the form to display on add and edit project
     *
     * @param string $project_id
     * @return Zend_Form
     */
    public function projectForm ($project_id = null)
    {
    	$project = new ProjectList();
    	$row = $project->find($project_id);
    	$project = $row[0];

        $statusList = new StatusList();
        $statusesTmp = $statusList->fetchAll("status_type='project'");
        $statuses = array();
        foreach($statusesTmp as $status){
        	$statuses[$status->status_id] = $status->status_name;
        }

        $categoryList = new CategoryList();
        $categoriesTmp = $categoryList->fetchAll();
        $categories = array();
        foreach($categoriesTmp as $cat){
        	$categories[$cat->category_id] = $cat->category_name;
        }

        $userList = new UserList();
        $usersTmp = $userList->fetchAll($userList->select());
		$users = array();
        foreach($usersTmp as $user){
        	$users[$user->user_id] = $user->last_name . ", " . $user->first_name . " (" . $user->username . ")";
        }

        $form = new Zend_Form();

        // Create and configure username element:
        $projectname = $form->createElement('text', 'project_name');
        $projectname->setLabel('Project Name')->setRequired(true);
        $projectstatus = $form->createElement('select', 'status_id');
        $projectstatus->setLabel("Status")->addMultiOptions($statuses)->setRequired(true);
        $projectdept = $form->createElement('select', 'category_id');
        $projectdept->setLabel("Category")->addMultiOptions($categories)->setRequired(true);
        $projectowner = $form->createElement('select', 'owner_id');
        $projectowner->setLabel("Owner")->addMultiOptions($users)->setRequired(true);
        $projectdesc = $form->createElement('textarea', 'project_desc');
        $projectdesc->setLabel('Project Desc')->setAttrib('cols', '50')->setAttrib('rows', '5');
        $startdate = $form->createElement('text', 'start_date');
        $startdate->setLabel("Start Date");
        $enddate = $form->createElement('text', 'end_date');
        $enddate->setLabel("End Date");

        // Add elements to form:
        $form->addElement($projectname)
             ->addElement($projectstatus)
             ->addElement($projectdept)
             ->addElement($projectowner)
             ->addElement($projectdesc)
             ->addElement($startdate)
             ->addElement($enddate)
             ->addElement('submit', 'save', array(
            'label' => 'Save'
        ));

        if (isset($project_id)) {
            $projectList = new ProjectList();
            $project = $projectList->find($project_id);
            $form->populate($project[0]->toArray());
            $action = '/project/edit/project/' . $project_id . '/';
        } else {
            $action = '/project/add/';
        }

        $form->setAction($action)->setMethod('post');

        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {

                $project = new project();

                $data = array(
                    'project_name' => $formData['project_name'],
                    'status_id' => $formData['status_id'],
                    'category_id' => $formData['category_id'],
                    'owner_id' => $formData['owner_id'],
                    'project_desc' => $formData['project_desc'],
                    'start_date' => $formData['start_date'],
                    'end_date' => $formData['end_date']
                );

                if (isset($project_id)) {
                    $project->update($data, "project_id='{$project_id}'");
                } else {
                    $project->insert($data);
                }

                $this->_redirect('/project/list/');
            } else {
                $form->populate($formData);
            }
        }

        return $form;
    }


    /**
     * Action to add a new project
     *
     */
    public function addAction ()
    {
        $this->view->pageTitle = "Add Project";
        $this->view->crumbTrail = "<a href='/project/list/'>Projects</a>::Add Project";
        $this->view->form = $this->projectForm();
    }


    /**
     * Action to edit the existing action
     *
     */
    public function editAction ()
    {
        $this->view->pageTitle = "Edit Project";
        $this->view->crumbTrail = "<a href='/project/list/'>Projects</a>::Edit Project";
        $this->view->form = $this->projectForm($this->_getParam('project'));
    }

    /**
     * Display a page that contains an overview of a single project
     *
     */
    public function overviewAction ()
    {
        //get project information
        $project = new ProjectList();
        $row = $project->find($this->_getParam('project_id'));
        $project = $row[0];
		$owner = $project->getOwner();$owner = $project->getOwner();

        $this->view->project_id = $project->project_id;
        $this->view->project_name = $project->project_name;
        $this->view->project_desc = $project->project_desc;
        $this->view->status_name = $project->getStatus()->status_name;
        $this->view->owner_name = $owner->last_name . ", " . $owner->first_name;

        //get tasks
        $taskOutput = '';
        $tasks = $project->getTasks();
        if($tasks->count() > 0){
			$taskOutput = "<ul>";
	        foreach($tasks as $task){
	        	$taskOutput .= "<li><a href='/task/edit/task_id/" . $task->task_id . "/'>" . $task->task_name . "</a></li>";
	        }
	        $taskOutput .= "</ul>";
        }

        $this->view->tasks = $taskOutput;
        include('FusionCharts/FusionCharts.php');

        $strXML = "
		<chart dateFormat='dd/mm/yyyy'
			hoverCapBorderColor='2222ff'
			hoverCapBgColor='e1f5ff'
			ganttWidthPercent='71'
			ganttLineAlpha='60'
			canvasBorderColor='024455'
			canvasBorderThickness='0'
			gridBorderColor='4567aa'
			gridBorderAlpha='20'>

		<categories  bgColor='009999'>
			<category start='1/3/2005' end='31/8/2005' align='center' name='" . $project->project_name . "'  fontColor='ffffff' isBold='1' fontSize='16' />
		</categories>
		<categories  bgColor='4567aa' fontColor='ff0000'>
			<category start='1/3/2005' end='31/8/2005' align='center' name='Months'  alpha='' font='Verdana' fontColor='ffffff' isBold='1' fontSize='16' />
		</categories>
		<categories  bgColor='ffffff' fontColor='1288dd' fontSize='10' >
			<category start='1/3/2005' end='31/3/2005' align='center' name='March'  isBold='1' />
			<category start='1/4/2005' end='30/4/2005' align='center' name='April'  isBold='1' />
			<category start='1/5/2005' end='31/5/2005' align='center' name='May' isBold='1' />
			<category start='1/6/2005' end='30/6/2005' align='center' name='June' isBold='1'/>
			<category start='1/7/2005' end='31/7/2005' align='center' name='July' isBold='1' />
			<category start='1/8/2005' end='31/8/2005' align='center' name='August' isBold='1'/>
		</categories>
		<processes headerText='Tasks' fontColor='ffffff' fontSize='10' isBold='1' isAnimated='1' bgColor='4567aa'  headerVAlign='right' headerbgColor='4567aa' headerFontColor='ffffff' headerFontSize='16' width='80' align='left'>
			<process Name='Wrting' id='1' />
			<process Name='Signing' id='2' />
			<process Name='Financing' id='3' />
			<process Name='Permission' id='4' />
			<process Name='Plumbing' id='5' />
			<process Name='Terrace' id='6' />
			<process Name='Inspection' id='7' />
			<process Name='Wood Work' id='8' />
			<process Name='Interiors' id='9' />
			<process Name='Shifting' id='10' />
		</processes>
		<dataTable showProcessName='1' nameAlign='left' fontColor='000000' fontSize='10' isBold='1' headerBgColor='00ffff' headerFontColor='4567aa' headerFontSize='11' vAlign='right' align='left'>
			<dataColumn width='70' headerfontcolor='ffffff' headerBgColor='4567aa' bgColor='eeeeee'  headerColor='ffffff' headerText='Start' isBold='0'>
				<text label='7/3/2005' />
				<text label='6/4/2005' />
				<text label='1/5/2005' />

				<text label='13/5/2005' />
				<text label='2/5/2005' />
				<text label='1/6/2005' />
				<text label='15/6/2005' />
				<text label='22/6/2005' />
				<text label='18/6/2005' />
				<text label='15/7/2005' />
			</dataColumn>
			<dataColumn width='70' headerfontcolor='ffffff'  bgColor='eeeeee' headerbgColor='4567aa'  fontColor='000000' headerText='Finish' isBold='0'>

				<text label='22/4/2005' />
				<text label='12/5/2005' />
				<text label='2/6/2005' />
				<text label='19/6/2005' />
				<text label='19/6/2005' />
				<text label='19/7/2005' />
				<text label='11/8/2005' />
				<text label='5/8/2005' />
				<text label='22/7/2005' />
				<text label='11/8/2005' />

			</dataColumn>

		</dataTable>
		<tasks  width='10' >
			<task name='Planned' processId='1' start='7/3/2005' end='18/4/2005' id='1-1' color='4567aa' height='10' topPadding='5' animation='0'/>
			<task name='Actual' processId='1' start='7/3/2005' end='22/4/2005' id='1' color='cccccc' alpha='100'  topPadding='19' height='10' />
			<task name='Planned' processId='8' start='22/6/2005' end='29/7/2005' id='2-1' color='4567aa' alpha='100'  height='10' topPadding='5' animation='0'/>
			<task name='Actual' processId='8' start='22/6/2005' end='5/8/2005' id='2' color='cccccc' alpha='100'  height='10' topPadding='19'/>
			<task name='Planned' processId='2' start='6/4/2005' end='2/5/2005' id='3-1' color='4567aa' height='10' topPadding='5' animation='0'/>
			<task name='Actual' processId='2' start='6/4/2005' end='12/5/2005' id='3' color='cccccc' alpha='100'  isAnimated='1' height='10' topPadding='19'/>
			<task name='Planned' processId='9' start='18/6/2005' end='21/7/2005' id='4-1' color='4567aa' height='10' topPadding='5' animation='0'/>

			<task name='Actual' processId='9' start='18/6/2005' end='22/7	/2005' id='4' color='cccccc' alpha='100'  isAnimated='1' height='10' topPadding='19'/>
			<task name='Planned' processId='3' start='1/5/2005' end='2/6/2005' id='5-1' color='4567aa' height='10' topPadding='5' animation='0'/>
			<task name='Actual' processId='3' start='1/5/2005' end='2/6/2005' id='5' color='cccccc' height='10' topPadding='19'/>
			<task name='Planned' processId='4' start='11/5/2005' end='12/6/2005' id='6-1' color='4567aa' height='10' topPadding='5' animation='0'/>
		  	<task name='Actual' processId='4' start='13/5/2005' end='19/6/2005' id='6' color='cccccc'  height='10' topPadding='19'/>
			<task name='Planned' processId='5' start='1/5/2005' end='12/6/2005' id='7-1' color='4567aa' height='10' topPadding='5' animation='0'/>
		  	<task name='Actual' processId='5' start='2/5/2005' end='19/6/2005' id='7' color='cccccc'  height='10' topPadding='19'/>
			<task name='Planned' processId='6' start='1/6/2005' end='12/7/2005' id='8-1' color='4567aa' height='10' topPadding='5' animation='0'/>
		  	<task name='Actual' processId='6' start='1/6/2005' end='19/7/2005' Id='8' color='cccccc' height='10' topPadding='19'/>

			<task name='Planned' processId='7' start='11/6/2005' end='7/8/2005' Id='9-1' color='4567aa' height='10' topPadding='5' animation='0'/>
		  	<task name='Actual' processId='7' start='15/6/2005' end='11/8/2005' Id='9' color='cccccc' height='10' topPadding='19'/>
			<task name='Planned' processId='10' start='11/7/2005' end='7/8/2005' Id='10-1' color='4567aa' height='10' topPadding='5' animation='0'/>
		  	<task name='Actual' processId='10' start='15/7/2005' end='11/8/2005' Id='10' color='cccccc' height='10' topPadding='19'/>

		</tasks>
		<connectors>
			<connector fromTaskId='3' toTaskId='5' color='4567aa' thickness='2' fromTaskConnectStart='1'/>
			<connector fromTaskId='8' toTaskId='9' color='4567aa' thickness='2' fromTaskConnectStart='1'/>
		</connectors>

		</chart>";
        $this->view->gantt = renderChartHTML('/FusionCharts/FCF_Gantt.swf', '', $strXML, 'gantt', '800', '500', true);

		$this->view->pageTitle = "Project Overview";
		$this->view->crumbTrail = "<a href='/project/list/'>Projects</a>::Project Overview";
    }
}